//登录
$(document).ready(function () {
    $(function () {
        var animationLibrary = 'animate';
        $.easing.easeOutQuart = function (x, t, b, c, d) {
            return -c * ((t = t / d - 1) * t * t * t - 1) + b;
        };
        $('[ripple]:not([disabled],.disabled)').on('mousedown', function (e) {
            var button = $(this);
            var touch = $('<touch><touch/>');
            var size = button.outerWidth() * 1.8;
            var complete = false;
            $(document).on('mouseup', function () {
                var a = {'opacity': '0'};
                if (complete === true) {
                    size = size * 1.33;
                    $.extend(a, {
                        'height': size + 'px',
                        'width': size + 'px',
                        'margin-top': -size / 2 + 'px',
                        'margin-left': -size / 2 + 'px'
                    });
                }
                touch[animationLibrary](a, {
                    duration: 500,
                    complete: function () {
                        touch.remove();
                    },
                    easing: 'swing'
                });
            });
            touch.addClass('touch').css({
                'position': 'absolute',
                'top': e.pageY - button.offset().top + 'px',
                'left': e.pageX - button.offset().left + 'px',
                'width': '0',
                'height': '0'
            });
            button.get(0).appendChild(touch.get(0));
            touch[animationLibrary]({
                'height': size + 'px',
                'width': size + 'px',
                'margin-top': -size / 2 + 'px',
                'margin-left': -size / 2 + 'px'
            }, {
                queue: false,
                duration: 500,
                'easing': 'easeOutQuart',
                'complete': function () {
                    complete = true;
                }
            });
        });
    });
    var username = $('#username'), password = $('#password'), erroru = $('erroru'), errorp = $('errorp'), submit = $('#submit'), udiv = $('#u'), pdiv = $('#p');
    username.blur(function () {
        if (username.val() == '') {
            udiv.attr('errr', '');
        } else {
            udiv.removeAttr('errr');
        }
    });
    password.blur(function () {
        if (password.val() == '') {
            pdiv.attr('errr', '');
        } else {
            pdiv.removeAttr('errr');
        }
    });
    submit.on('click', function (event) {
        event.preventDefault();
        if (username.val() == '') {
            udiv.attr('errr', '');
        } else {
            udiv.removeAttr('errr');
        }
        if (password.val() == '') {
            pdiv.attr('errr', '');
        } else {
            pdiv.removeAttr('errr');
        }
    });
});

$(function () {
    //登录输入框效果
    $('.form_text_ipt input').focus(function () {
        $(this).parent().css({
            'box-shadow': '0 0 3px #bbb',
        });
    });
    $('.form_text_ipt input').blur(function () {
        $(this).parent().css({
            'box-shadow': 'none',
        });
        //$(this).parent().next().hide();
    });

    /* //表单验证
     $('.form_text_ipt input').bind('input propertychange',function(){
     if($(this).val()==""){
     $(this).css({
     'color':'red',
     });
     $(this).parent().css({
     'border':'solid 1px red',
     });
     //$(this).parent().next().find('span').html('helow');
     $(this).parent().next().show();
     }else{
     $(this).css({
     'color':'#ccc',
     });
     $(this).parent().css({
     'border':'solid 1px #ccc',
     });
     $(this).parent().next().hide();
     }
     });*/

    //登录、注册界面切换
    $("#loginer").on("click", function () {
        $(".loginer").css({
            display: "block"
        });
        $(".loginer").addClass("animated bounceIn");
        $(".wrap").css({
            display: "none"
        });
    });
    $("#wrap").on("click", function () {
        $(".wrap").css({
            display: "block"
        });
        $(".wrap").addClass("animated bounceIn")
        $(".loginer").css({
            display: "none"
        });
    });
    //切换
    $("#nowLogin").on("click", function () {
        $(".loginer").css({
            display: "block"
        });
        $(".wrap").css({
            display: "none"
        });
    });
    $("#nowWrap").on("click", function () {
        $(".wrap").css({
            display: "block"
        });
        $(".loginer").css({
            display: "none"
        });
    });
    //删除
    $("#loginDele").on("click", function () {
        $(".loginer").css({
            display: "none"
        });
    });
    $("#regsiterDele").on("click", function () {
        $(".wrap").css({
            display: "none"
        });
    });


    //拖动鼠标移动登录注册
    var login_title = $("#login_title");
    var regsiter = $("#regsiter");
    var loginDraw = $("#loginDraw");
    var mustLogin = $("#mustLogin");

    draw(login_title, regsiter);
    draw(loginDraw, mustLogin);
    function draw(drawDom, moveDom) {
        drawDom.on("mousedown", function (event) {
            var event = event || window.event;
            var pageX = event.pageX || event.clientX + document.documentElement.scrollLeft;
            var pageY = event.pageY || event.clientY + document.documentElement.scrollTop;
            var spaceX = pageX - parseInt(moveDom.offset.left) || 0;
            var spaceY = pageY - parseInt(moveDom.offset.top) || -200;
            document.onmousemove = function (event) {
                var event = event || window.event;
                var pageX = event.pageX || event.clientX + document.documentElement.scrollLeft;
                var pageY = event.pageY || event.clientY + document.documentElement.scrollTop;
                moveDom.css({
                    left: pageX - spaceX + "px",
                    top: pageY - spaceY + "px"
                });
                //清理选中的文字
                window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
            }
        });
        document.onmouseup = function () {
            document.onmousemove = null;
        }

    }
})

//登录的数据
$(function () {
    var regMobile = /^(13[0-9]|14[57]|15[0-9]|17[0-9]|18[0-9])\d{8}$/;
    var regEmail = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
    regExp($("#username"));
    function regExp(inp) {

        inp.on('blur', function () {
            $("#logSuccee").removeClass("glyphicon glyphicon-ok glyphicon-remove succee error");
            if (regMobile.test($(this).val())) {
                $("#logSuccee").addClass("glyphicon glyphicon-ok succee")
            } else if (regEmail.test($(this).val())) {
                $("#logSuccee").addClass("glyphicon glyphicon-ok succee")
            } else {
                $("#logSuccee").addClass("glyphicon glyphicon-remove error")
            }
        });
    }


    $("#submit").on("click", function () {
        var username = $("#username").val();
        var password = $("#password").val();
        var button = $("#submit").val();
        console.log(username);
        console.log(password);
        console.log($("#submit").val());
        $.ajax({
            url: "login.php",
            type: "post",
            dataType: "json",
            data: {username: username, password: password, button: button},
            beforeSend: function () {

            },
            success: function (data) {
                 console.log(data);
                $("#loginDele").on("click", function () {
                    $(".loginer").css({
                        display: "none"
                    });
                });
                $("#loginDele").click();
                alert("登录成功");
             window.location.href = 'liujiazhuang/pic.html'
                $("#username").val("");
                $("#password").val("");
            },
            error: function (data) {
                alert("您的用户名和密码不匹配");
                 console.log(data);
            }
        });

    });


})

//注册的数据
$(function () {
    var regMobile = /^(13[0-9]|14[57]|15[0-9]|17[0-9]|18[0-9])\d{8}$/;
    var regEmail = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
    regExp($("#regMob"));
    function regExp(inp) {
        inp.on('blur', function () {
            $("#regstyle").removeClass("glyphicon glyphicon-ok glyphicon-remove succee error");
            if (regMobile.test($(this).val())) {
                $("#regstyle").addClass("glyphicon glyphicon-ok succee")
            } else if (regEmail.test($(this).val())) {
                $("#regstyle").addClass("glyphicon glyphicon-ok succee")
            } else {
                $("#regstyle").addClass("glyphicon glyphicon-remove error")
            }
        });
    }

    $("#psw2").on("blur", function () {
        $("#resetstyle").removeClass("glyphicon glyphicon-ok glyphicon-remove succee error");
        if ($("#psw2").val() === $("#psw1").val() && $("#psw1").val() !== "") {
            $("#resetstyle").addClass("glyphicon glyphicon-ok succee")
        } else {
            $("#resetstyle").addClass("glyphicon glyphicon-remove error")
        }
    });

    $("#regsitBtn").on("click", function () {
        var username = $("#regMob").val();
        var password = $("#psw2").val();
        var button = $("#regsitBtn").val();
        // console.log(username);
        // console.log(password);
        // console.log($("#regsitBtn").val());

        $.ajax({
            url: "regist.php",
            type: "post",
            dataType: "json",
            data: {username: username, password: password, button: button},
            beforeSend: function () {

            },
            success: function (data) {
                //console.log(data);
                if (data === 1) {
                    $("#nowLogin").on("click", function () {

                        $(".loginer").css({
                            display: "block"
                        });
                        $(".wrap").css({
                            display: "none"
                        });
                    });
                    $("#nowLogin").click();
                    alert("恭喜你注册成功，请登录");
                }
                $("#regMob").val("");
                $("#psw2").val("");

            },
            error: function (data) {
                alert(data);
            }
        });

    });
})